<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Div with Dynamic Inner Fade Effect</title>
<style>
  body {
    margin: 0;
    padding: 20px;
    font-family: Arial, sans-serif;
    background-color: #f0f0f0;
    display: flex;
    justify-content: center;
    align-items: center;
    height: 100vh;
  }
  
  .container {
    position: relative;
    width: 300px;
    height: 400px;
    overflow: hidden; /* 隐藏溢出的内容 */
    background-color: #fff;
    box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
  }

  .content {
    width: 100%;
    height: 100%;
    overflow-y: auto;
  }

  .content-inner {
    padding: 20px;
  }

  .fade-overlay {
    position: absolute;
    pointer-events: none; /* 确保鼠标事件穿透到下面的内容 */
    transition: opacity 0.3s; /* 添加平滑过渡效果 */
  }

  .fade-overlay.top,
  .fade-overlay.bottom {
    left: 0;
    right: 0;
    height: 20px; /* 调整高度以达到所需的虚化宽度 */
  }

  .fade-overlay.top {
    top: 0;
    background: linear-gradient(to bottom, rgba(255, 255, 255, 1), rgba(255, 255, 255, 0));
  }

  .fade-overlay.bottom {
    bottom: 0;
    background: linear-gradient(to top, rgba(255, 255, 255, 1), rgba(255, 255, 255, 0));
  }

  .fade-hidden {
    opacity: 0;
  }
</style>
</head>
<body>

<div class="container">
  <div class="content">
    <div class="content-inner">
      <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua...</p>
      <!-- 添加更多内容 -->
      <p>Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur...</p>
      <p>Curabitur pretium tincidunt lacus. Nulla gravida orci a odio...</p>
      <p>Vivamus fermentum semper porta. Nunc diam velit, adipiscing ut tristique vitae, sagittis vel odio...</p>
      <p>Nam dictum, odio nec pretium volutpat, arcu ante placerat erat...</p>
    </div>
  </div>
  <div class="fade-overlay top"></div>
  <div class="fade-overlay bottom"></div>
</div>

<script>
  document.addEventListener('DOMContentLoaded', function() {
    const content = document.querySelector('.content');
    const topFade = document.querySelector('.fade-overlay.top');
    const bottomFade = document.querySelector('.fade-overlay.bottom');

    function updateFade() {
      if (content.scrollTop === 0) {
        topFade.classList.add('fade-hidden');
      } else {
        topFade.classList.remove('fade-hidden');
      }

      if (content.scrollHeight - content.scrollTop === content.clientHeight) {
        bottomFade.classList.add('fade-hidden');
      } else {
        bottomFade.classList.remove('fade-hidden');
      }
    }

    // 初始化时调用一次
    updateFade();

    // 在滚动时更新
    content.addEventListener('scroll', updateFade);
  });
</script>

</body>
</html>
